﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Twitter_Data_Crawler
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnTaoDuLieuTest_Click(object sender, EventArgs e)
        {
            frmDuLieuTest frmTest = new frmDuLieuTest();
            this.Hide();
            frmTest.ShowDialog();
            this.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmPhanTich frmPT = new frmPhanTich();
            this.Hide();
            frmPT.ShowDialog();
            this.Show();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                string strSQL = "";
                DataTable objData;
                SaveFileDialog dlgSave;
                StreamWriter objWriter;
                double dblTemp;
                string strTemp = "";
                string strTempConclude = "";
                bool blnTemp;

                strSQL += "Select tUser.*, tFeature.* From tUser, tFeature Where tUser.UserID = tFeature.UserID";

                objData = Program.gobjDb.fncReadData(strSQL);

                if (objData == null) return;

                dlgSave = new SaveFileDialog();
                dlgSave.Filter = "Weka data file|*.arff";
                dlgSave.DefaultExt = ".arff";

                if (dlgSave.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

                objWriter = new StreamWriter(dlgSave.FileName);

                objWriter.WriteLine("@relation twitterspam.symbolic");
                //objWriter.WriteLine("@attribute Username string");
                objWriter.WriteLine("@attribute FoF {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute Url {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute Similar {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute Mention {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute Hashtag {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute Retweet {Lv1, Lv2, Lv3, Lv4, Lv5, Lv6, Lv7, Lv8}");
                objWriter.WriteLine("@attribute FsubF {negative, 0-50, 51-100, 101-150, 151-200, 201-250, 251-300, 301-500, 501-1000, over1000}");
                objWriter.WriteLine("@attribute Conclude {True, False}");
                objWriter.WriteLine("");
                objWriter.WriteLine("@data");

                for (int i = 0; i <= objData.Rows.Count - 1; i++)
                {
                    strTemp = "";

                    //strTemp += "\"" + clsCommon.fncCnvNullToString(objData.Rows[i]["ScreenName"]) + "\"" + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_FoF"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_Url"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_Similar"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_Mention"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_HashTag"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_Retweet"]);
                    strTemp += xDanhGia1(dblTemp) + "\t,";

                    dblTemp = clsCommon.fncCnvNullToDbl(objData.Rows[i]["Index_FsubF"]);
                    strTemp += xDanhGia3(dblTemp) + "\t,";

                    strTempConclude = clsCommon.fncCnvNullToString(objData.Rows[i]["Conclude"]);
                    if (clsCommon.fncIsBlank(strTempConclude))
                    {
                        strTemp += "?";
                    }
                    else
                    {
                        blnTemp = clsCommon.fncCnvNullToBool(strTempConclude);
                        if (blnTemp) { strTemp += "True"; } else { strTemp += "False"; }
                    }
                    

                    objWriter.WriteLine(strTemp);
                }
                                
                objWriter.Flush();
                objWriter.Close();

                clsCommon.fncMsgInfo("Done!");
            }
            catch (Exception ex) { clsCommon.fncMsgError(ex.Message); }
        }

        private string xDanhGia1(double dblGrade)
        {
            string strResult = "?";

            try
            {
                if (dblGrade < 0.4) { strResult = "Lv1"; return strResult; }
                if (dblGrade >= 0.4 && dblGrade < 0.8) { strResult = "Lv2"; return strResult; }
                if (dblGrade >= 0.8 && dblGrade < 1.2) { strResult = "Lv3"; return strResult; }
                if (dblGrade >= 1.2 && dblGrade < 1.6) { strResult = "Lv4"; return strResult; }
                if (dblGrade >= 1.6 && dblGrade < 2.0) { strResult = "Lv5"; return strResult; }
                if (dblGrade >= 2.0 && dblGrade < 2.5) { strResult = "Lv6"; return strResult; }
                if (dblGrade >= 2.5 && dblGrade < 3.0) { strResult = "Lv7"; return strResult; }
                if (dblGrade >= 3.0) { strResult = "Lv8"; return strResult; }
            }
            catch (Exception ex) { clsCommon.fncMsgError(ex.Message); }
            return strResult;
        }

        private string xDanhGia2(double dblGrade)
        {
            string strResult = "?";

            try
            {
                if (dblGrade <= 0.1) { strResult = "Low"; return strResult; }
                if (dblGrade > 0.1 && dblGrade <= 0.2) { strResult = "Medium"; return strResult; }
                if (dblGrade > 0.2) { strResult = "High"; return strResult; }
            }
            catch (Exception ex) { clsCommon.fncMsgError(ex.Message); }
            return strResult;
        }

        private string xDanhGia3(double dblGrade)
        {
            string strResult = "?";

            try
            {
                if (dblGrade <= 0) { strResult = "negative"; return strResult; }
                if (dblGrade > 0 && dblGrade <= 50) { strResult = "0-50"; return strResult; }
                if (dblGrade > 50 && dblGrade <= 100) { strResult = "51-100"; return strResult; }
                if (dblGrade > 100 && dblGrade <= 150) { strResult = "101-150"; return strResult; }
                if (dblGrade > 150 && dblGrade <= 200) { strResult = "151-200"; return strResult; }
                if (dblGrade > 200 && dblGrade <= 250) { strResult = "201-250"; return strResult; }
                if (dblGrade > 250 && dblGrade <= 300) { strResult = "251-300"; return strResult; }
                if (dblGrade > 300 && dblGrade <= 500) { strResult = "301-500"; return strResult; }
                if (dblGrade > 500 && dblGrade <= 1000) { strResult = "501-1000"; return strResult; }
                if (dblGrade > 1000) { strResult = "over1000"; return strResult; }
            }
            catch (Exception ex) { clsCommon.fncMsgError(ex.Message); }
            return strResult;
        }
    }
}
